/**
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
*/

package org.elephantt.webby;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletRequest;

/**
 * Utilities related to servlet requests
 */
public class RequestUtil {

  /**
   * Puts the model into the request attributes. The standard way is to set a single attribute called "model"
   * so as to not interfere with other request attributes. An alternative design would be to set all the keys
   * from model flat, but I dislike this approach.
   */
  public static void copyModelToRequestAttributes(Object model, HttpServletRequest request) {
    request.setAttribute("model", model);
  }

  /**
   * Marks a request as being "internal". This is useful for helping downstream servlets (such as forwarding
   * a request to a JSP servlet for rendering) determine whether to honor the request.
   * (direct, "external" requests for a .jsp page should be denied typically).
   */
  public static void markRequestInternal(ServletRequest request) {
    request.setAttribute(ServletRequestAttributes.INTERNAL, true);
  }

  /**
   * Returns true if the request has been marked as internal.
   */
  public static boolean isRequestInternal(ServletRequest request) {
    Object attr = request.getAttribute(ServletRequestAttributes.INTERNAL);
    return (attr != null && attr.equals(true));
  }
}